package com.next.aappublicapp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.next.aappublicapp.util.FacebookUtil;
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class AapApplication extends Application{
	public static final String SHARED_FILE_NAME = "shared.png";

	@Override
	public void onCreate(){
		super.onCreate();
		//Print Facebook SHA signature for debug purpose
		FacebookUtil.printShaSignatureForFacebook(getApplicationContext());
		copyPrivateRawResourceToPubliclyAccessibleFile();

		//Initiate ImageLoader
		if(!ImageLoader.getInstance().isInited()){
			ImageLoaderConfiguration.Builder imageLoaderConfigurationBuilder = new ImageLoaderConfiguration.Builder(this);
			//imageLoaderConfigurationBuilder.discCacheSize(1024 * 1024 * 10);//10mb
			Log.i("Ravi", "Environment.getExternalStorageState()="+Environment.getExternalStorageState());
			Log.i("Ravi", "Environment.isExternalStorageRemovable()="+Environment.isExternalStorageRemovable());
			Log.i("Ravi", "getExternalCacheDir()="+getExternalCacheDir());
			Log.i("Ravi", "getCacheDir()="+getCacheDir());
			Log.i("Ravi", "Environment.getExternalStorageDirectory()()="+Environment.getExternalStorageDirectory());
			
			final String cachePath = getCacheDir().getPath();
			/*
		            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
		                    !Environment.isExternalStorageRemovable() ? getExternalCacheDir().getPath() :
		                            getCacheDir().getPath();
		                            */
	        imageLoaderConfigurationBuilder.discCache(new TotalSizeLimitedDiscCache(new File(Environment.getExternalStorageDirectory()+"/data/aap/images"), 1024 * 1024 * 10));
	        imageLoaderConfigurationBuilder.threadPoolSize(5);
	        
	        imageLoaderConfigurationBuilder.memoryCache(new UsingFreqLimitedMemoryCache(2000000));
	        
	     // Creates display image options for custom display task
	        DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder();
	        //displayImageOptionsBuilder.showStubImage(R.drawable.image_not_available);
            //displayImageOptionsBuilder.showImageForEmptyUrl(R.drawable.image_not_available);
            //displayImageOptionsBuilder.cacheInMemory();
            displayImageOptionsBuilder.cacheOnDisc();
            displayImageOptionsBuilder.imageScaleType(ImageScaleType.IN_SAMPLE_INT);
            //displayImageOptionsBuilder.decodingType(DecodingType.MEMORY_SAVING)
            ;
	        
	        imageLoaderConfigurationBuilder.defaultDisplayImageOptions(displayImageOptionsBuilder.build());
	        Log.i("Ravi", "cachePath="+cachePath);
			//imageLoaderConfigurationBuilder.enableLogging();
			ImageLoader.getInstance().init(imageLoaderConfigurationBuilder.build());
		}
		

		
	}
	/**
     * Copies a private raw resource content to a publicly readable
     * file such that the latter can be shared with other applications.
     */
    private void copyPrivateRawResourceToPubliclyAccessibleFile() {
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = getResources().openRawResource(R.drawable.ic_launcher);
            outputStream = openFileOutput(SHARED_FILE_NAME,
                    Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
            byte[] buffer = new byte[1024];
            int length = 0;
            try {
                while ((length = inputStream.read(buffer)) > 0){
                    outputStream.write(buffer, 0, length);
                }
            } catch (IOException ioe) {
                /* ignore */
            }
        } catch (FileNotFoundException fnfe) {
            /* ignore */
        } finally {
            try {
                inputStream.close();
            } catch (IOException ioe) {
               /* ignore */
            }
            try {
                outputStream.close();
            } catch (IOException ioe) {
               /* ignore */
            }
        }
    }

}
